package com.example;

import java.io.IOException;
import java.util.Random;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class KNNMapper extends Mapper<LongWritable, Text, Text, Text> {
    private Random random = new Random();

    @Override
    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] parts = value.toString().split(",");
        // 假设特征位于13, 14, 15列，标签在第2列
        String features = parts[13] + "," + parts[14] + "," + parts[15];
        String label = parts[59];

        String datasetType = random.nextDouble() < 0.8 ? "train" : "test";
        context.write(new Text(datasetType), new Text(features + "," + label));
    }
}
